package com.sugon.controller.device;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.sugon.annotation.AutoLog;
import com.sugon.entity.DevParameterEntity;
import com.sugon.entity.DevTypeEntity;
import com.sugon.service.DevParameterService;
import com.sugon.service.DevTypeService;
import com.sugon.utils.PageUtils;
import com.sugon.utils.Query;
import com.sugon.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * 设备业务参数字段维护,用于从MES表中提取业务字段Controller
 *
 * @author YuXD
 * @date 2020-05-23 10:24:08
 */
@Controller
@RequestMapping("devparameter")
public class DevParameterController {

    @Autowired
    private DevParameterService devParameterService;
    @Autowired
    private DevTypeService devTypeService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<DevParameterEntity> devParameterList = devParameterService.queryList(query);
        int total = devParameterService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(devParameterList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @ResponseBody
    public R info(@PathVariable("id") String id) {
        DevParameterEntity devParameter = devParameterService.queryObject(id);

        return R.ok().put("devParameter", devParameter);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @ResponseBody
    public R save(@RequestBody DevParameterEntity devParameter) {
        // 校验设备类别是否存在
        DevTypeEntity devType = devTypeService.queryObjectByCode(devParameter.getDevType());
        if (devType == null) {
            return R.error("非法设备类别[" + devParameter.getDevType() + "]");
        }
        devParameter.setDevTypeName(devType.getTypeName());
        // 校验是否已设置
        List<DevParameterEntity> devParams = devParameterService.queryList(BeanUtil.beanToMap(devParameter));
        if(CollectionUtil.isNotEmpty(devParams)){
            return R.error("参数已设置,请确认!");
        }
        return devParameterService.save(devParameter) > 0 ? R.ok() : R.error();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @ResponseBody
    public R update(@RequestBody DevParameterEntity devParameter) {
        // 校验设备类别是否存在
        DevTypeEntity devType = devTypeService.queryObjectByCode(devParameter.getDevType());
        if (devType == null) {
            return R.error("非法设备类别[" + devParameter.getDevType() + "]");
        }
        devParameter.setDevTypeName(devType.getTypeName());
        // 校验是否已设置
        List<DevParameterEntity> devParams = devParameterService.queryList(BeanUtil.beanToMap(devParameter));
        if(CollectionUtil.isNotEmpty(devParams)){
            return R.error("参数已设置,请确认!");
        }
        devParameterService.update(devParameter);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @ResponseBody
    public R delete(@RequestBody String[] ids) {
        devParameterService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<DevParameterEntity> list = devParameterService.queryList(params);

        return R.ok().put("list", list);
    }

    @RequestMapping("/imports")
    @ResponseBody
    public R imports(@RequestParam("file") MultipartFile file) throws Exception {
        devParameterService.imports(file);
        return R.ok();
    }
}
